package com.lingshi.shopping_manager_api.controller;

import com.lingshi.shopping.entity.Specification;
import com.lingshi.shopping.entity.SpecificationOption;
import com.lingshi.shopping.entity.SpecificationOptions;
import com.lingshi.shopping.result.BaseResult;
import com.lingshi.shopping.service.SpecificationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 商品规格管理
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {

    @DubboReference
    private SpecificationService specificationService;

    /**
     * 新增规格
     * @param specification 规格对象
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Specification specification) {
        specificationService.add(specification);
        return BaseResult.success();
    }

    /**
     * 修改规格
     * @param specification 规格对象
     * @return
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Specification specification) {
        specificationService.update(specification);
        return BaseResult.success();
    }

    /**
     * 删除规格（批量删除）
     * @param ids 规格id
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long[] ids) {
        specificationService.delete(ids);
        return BaseResult.success();
    }

    /**
     * 根据id查询规格
     * @param id 主键id
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Long id) {
        Specification specification = specificationService.findById(id);
        return BaseResult.success(specification);
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @return 规格分页对象
     */
    @GetMapping("/search")
    public BaseResult search(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        return BaseResult.success(specificationService.search(page,size));
    }

    /**
     * 根据产品id查询所有规格
     * @param id 产品id
     * @return
     */
    @GetMapping("/findByProductTypeId")
    public BaseResult findByProductTypeId(Long id) {
        return BaseResult.success(specificationService.findByProductTypeId(id));
    }

    /**
     * 新增规格项
     * @param specificationOptions 规格项参数
     * @return
     */
    @PostMapping("/addOption")
    public BaseResult addOption(@RequestBody SpecificationOptions specificationOptions) {
        specificationService.addOption(specificationOptions);
        return BaseResult.success();
    }

    /**
     * 删除规格项
     * @param ids 规格项id
     * @return
     */
    @DeleteMapping("/deleteOption")
    public BaseResult deleteOption(Long[] ids) {
        specificationService.deleteOption(ids);
        return BaseResult.success();
    }

}
